var prefix = jsctx + "/coupon/apply";

var couponApply = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageCouponApply",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            offset: (params.offset / params.limit) + 1,
                            limit: params.limit,
                            operatorId: $("#operatorId").val(),
                            operatorName: $("#operatorName").val()
                        };
                    },
                    columns: [
                        // {
                        //     title: 'id', field: '', width: '50', align: 'left',
                        //     formatter: function (value, row, index) {
                        //         return index + 1;
                        //     }
                        // },
                        {field: 'couponTemplateApplyId', title: 'ID', width: '100', align: 'center'},
                        {field: 'operatorName', title: '加盟商', width: '100', align: 'center'},
                        {field: 'description', title: '卡劵描述', width: '100', align: 'center'},
                        {
                            field: 'applyStatus',
                            title: '审核状态',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">待审核</span>';
                                } else if (value == '1') {
                                    return '<span class="label label-info">审核通过</span>';
                                } else if (value == '2') {
                                    return '<span class="label label-danger">审核未通过</span>';
                                }
                            }
                        },

                        {
                            title: '操作',
                            field: 'roleId',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-primary btn-sm' + has_edit + '" href="#" title="通过" onclick="couponApply.updateApply(\''
                                    + row.couponTemplateApplyId + '\',1)">通过</a>';
                                var b = '<a class="btn btn-warning btn-sm' + has_edit + '" href="#" title="驳回" onclick="couponApply.updateApply(\''
                                    + row.couponTemplateApplyId + '\',2)">驳回</a>';
                                var c = '<a class="btn btn-primary btn-sm' + has_edit + '" href="#" title="删除" onclick="couponApply.delete(\''
                                    + row.couponTemplateApplyId + '\')">删除</a>';
                                if (row.applyStatus == '0') {
                                    return a + b;
                                }
                                return c;


                            }
                        }
                    ]
                });
    }


    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function add(operatorId) {
        operatorId = 2;
        location.href = prefix + "/add/" + operatorId;
    }

    function deleteCouponApply(id) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteCouponApply",
                type: "post",
                data: {
                    'id': id
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function updateApply(id, status) {
        layer.confirm('确定操作该记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateCouponApplyForAudit",
                type: "post",
                data: {
                    'id': id,
                    'status': status
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function exportInfo() {
        $.fileDownload(prefix + "/exporListPageCouponApply", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                pageSize: $(".page-size").text(),
                pageNo: $('.active .page-link').text(),

                operatorId: $("#operatorId").val(),
                operatorName: $("#operatorName").val()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    };

    return {
        init: init,
        reLoad: reLoad,
        add: add,
        delete: deleteCouponApply,
        updateApply:updateApply,
        exportInfo:exportInfo
    };
})();

var addCouponApply = (function () {
    function init() {
        validateRule();
    };


    var isajaxsend = true;

    function save() {
        var formData = new FormData($("#frm")[0]);
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/addCouponApply",
            data: formData,
            processData: false,
            contentType: false,
            async: false,
            beforeSend: function () {
                isajaxsend = false
            },
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    window.history.back();
                } else {
                    parent.layer.alert(data.message)
                }

            },
            complete: function () {
                isajaxsend = false
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frm").validate({
            rules: {
                content: {
                    maxlength: 120
                }

            },
            messages: {
                content: {
                    maxlength: icon + "最大内容为120位"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();


var couponApplyWithOerator = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageCouponApply",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            offset: (params.offset / params.limit) + 1,
                            limit: params.limit,
                            operatorId: $("#operatorId").val(),
                            operatorName: $("#operatorName").val()
                        };
                    },
                    columns: [
                        // {
                        //     title: 'id', field: '', width: '50', align: 'left',
                        //     formatter: function (value, row, index) {
                        //         return index + 1;
                        //     }
                        // },
                        {field: 'couponTemplateApplyId', title: 'ID', width: '100', align: 'center'},
                        {field: 'description', title: '卡劵描述', width: '100', align: 'center'},
                        {
                            field: 'applyStatus',
                            title: '审核状态',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">待审核</span>';
                                } else if (value == '1') {
                                    return '<span class="label label-info">审核通过</span>';
                                } else if (value == '2') {
                                    return '<span class="label label-danger">审核未通过</span>';
                                }
                            }
                        },
                        {field: 'createdTime', title: '申请时间', width: '100', align: 'center'},
                        {
                            field: 'lastUpdatedTime', title: '审批时间', width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.applyStatus != '0') {
                                    return value;
                                } else {
                                    return ;
                                }
                            }
                        }
                    ]
                });
    }


    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function add(operatorId) {
        operatorId = 2;
        location.href = prefix + "/add/" + operatorId;
    }

    function deleteCouponApply(id) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteCouponApply",
                type: "post",
                data: {
                    'id': id
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function updateApply(id, status) {
        layer.confirm('确定操作该记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateCouponApplyForAudit",
                type: "post",
                data: {
                    'id': id,
                    'status': status
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function exportInfo() {
        $.fileDownload(prefix + "/exporListPageCouponApply", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                pageSize: $(".page-size").text(),
                pageNo: $('.active .page-link').text(),

                operatorId: $("#operatorId").val(),
                operatorName: $("#operatorName").val()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    };

    return {
        init: init,
        reLoad: reLoad,
        add: add,
        delete: deleteCouponApply,
        updateApply:updateApply,
        exportInfo:exportInfo
    };
})();

